package com.atguigu.serviceedu.controller;


import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.vo.ChapterVO;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.impl.EduChapterServiceImpl;
import com.atguigu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-06-18
 */
@RestController
@RequestMapping("/serviceedu/educhapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;
    @GetMapping("/getNestByCourseId/{id}")
    public ResultVO getNestByCourseId(@PathVariable("id") String id){
        List<ChapterVO> chapterVOList = eduChapterService.queryNestByCourseId(id);
        return ResultVO.ok().Msg("课程章节获取成功").Data("items",chapterVOList);
    }
    @DeleteMapping("/removeChapterById/{id}")
    public ResultVO removeChapterById(@PathVariable("id")String id){
        boolean b = eduChapterService.deleteChapterById(id);
        if (b){
            return ResultVO.ok().Msg("删除章节成功").Data("flag",b);
        }else {
            return ResultVO.error().Msg("删除章节失败").Data("flag",b);
        }
    }

    @PostMapping("/addChapter")
    public ResultVO addChapter(@RequestBody EduChapter eduChapter){
        boolean save = eduChapterService.save(eduChapter);
        return ResultVO.ok().Msg("保存课程章节成功").Data("flag",save);
    }
    @PutMapping("/editChapterById")
    public ResultVO editChapterByid(@RequestBody EduChapter eduChapter){
        int i = eduChapterService.saveChapterById(eduChapter);
        if (i>0){
            return ResultVO.ok().Msg("修改章节信息成功");
        }else {
            return ResultVO.error().Msg("修改章节信息失败");
        }
    }
    @GetMapping("/getChapterById/{chapterId}")
    public ResultVO getChapterById(@PathVariable("chapterId")String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ResultVO.ok().Msg("回显章节信息成功").Data("Educhapter",eduChapter);
    }

}

